// pages/order/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        loading: true,
        list: [],
        page: 1,
        value: '',
        brand_title:'',
        theme_color:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.get_list()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (wx.getStorageSync('packge')) {
            this.agin()
        }
        if(app.dlc.gain('brand')){
          this.setData({
            brand_title:app.dlc.gain('brand_title')
          })
        }
        this.setData({
          theme_color:app.dlc.gain('theme_color')
        })
        this.getTabBar().init()
    },
    ChangeSearch(e) {
        this.setData({
            value: e.detail,
        });
    },
    onSearch() {
        this.setData({
            page: 1,
            list: [],
        })
        this.get_list()
    },
    onChange(e) {
        this.setData({
            active: e.detail.index
        })
        this.agin()
    },
    get_list() {
        const that = this;
        this.setData({
            loading: true
        })
        wx.removeStorageSync('packge')
        app.wxRequest('/api/Pack/index', {
            page: that.data.page,
            pack_status: that.data.active * 1 + 1,
            order_number: that.data.value
        }, function (res) {
            if (res.code == 200) {
                let list = res.data;
                if (list.length > 0 && list.length == 10) {
                    let page = that.data.page;
                    page++;
                    that.setData({
                        page: page
                    })
                } else {
                    that.setData({
                        loading: false
                    })
                }
                if (that.data.page == 1) {
                    that.setData({
                        list: list
                    })
                } else {
                    that.setData({
                        list: that.data.list.concat(list)
                    })
                }
            } else {
                app.toast(res.message)
            }
        })
    },
    agin() {
        this.setData({
            list: [],
            loading: true,
            page: 1
        })
        this.get_list()
    },
    copy_number(e) {
        let order_num = e.currentTarget.dataset.ordernum;
        wx.setClipboardData({
            data: order_num,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        app.toast('复制成功')
                        // console.log(res.data) // data
                    }
                })
            }
        })
    },
    order_details(e) {
        let index = e.currentTarget.dataset.index;
        let id = this.data.list[index].id
        wx.setStorageSync('orderitem', JSON.stringify(this.data.list[index]))
        wx.navigateTo({
            url: '/personal_center/pages/order_details/index?id=' + id,
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        wx.stopPullDownRefresh();
        this.agin()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.loading) {
            this.get_list()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})